package com.wyp168.leetcode;

/**
 * @ClassName AddBinarySolution
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2022/10/27 19:48
 */
public class AddBinarySolution {
    public static void main(String[] args) {
        AddBinarySolution solution = new AddBinarySolution();
        String a = "1010";
        String b = "1011";
        System.out.println(solution.addBinary(a, b));
    }

    public String addBinary(String a, String b) {
        int indexA = a.length() - 1;
        int indexB = b.length() - 1;
        String result = "";
        int up = 0;
        while (indexA >= 0 || indexB >= 0) {
            int intA = indexA >= 0 ? a.charAt(indexA) - 48 : 0;
            int intB = indexB >= 0 ? b.charAt(indexB) - 48 : 0;
            int temp = intA + intB + up;
            result = temp % 2 + result;
            up = temp > 1 ? 1 : 0;
            indexA --;
            indexB --;
        }
        return up == 0 ? result : up + result;
    }
}
